// index.js
const api = require('../../api/index'); // 头部引入

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    titleName: ["全部订单","待接单","已接单","已完成", "已取消"],
    orderTypeList: ["堂食", "店内自取", "配送到家"],
    statusList: ["", "", "", "", "", "已完成", "已取消", ""],
    orderList: [],
    page: 1,
    pageSize: 10,
    status: "",
    isRequest: true,
  },
  //切换订单选项
  onChange(event) {
    console.log(event);
    this.setData({
      orderList: [],
      page: 1,
      pageSize: 10,
      active: event.detail.index,
      isRequest:true
    })
    if (event.detail.index == 0) {
      this.orderHistory(this.data.page, this.data.pageSize)
      this.setData({
        status: ""
      })
    } else if (event.detail.index == 1) {
      this.orderHistory(this.data.page, this.data.pageSize, 2)
      this.setData({
        status: 2
      })
    }else if (event.detail.index == 2) {
      this.orderHistory(this.data.page, this.data.pageSize, 3)
      this.setData({
        status: 3
      })
    }else if (event.detail.index == 3) {
      this.orderHistory(this.data.page, this.data.pageSize, 5)
      this.setData({
        status: 5
      })
    } else {
      this.orderHistory(this.data.page, this.data.pageSize, 6)
      this.setData({
        status: 6
      })
    }
  },
  //前往订单详情页面
  goOrderDetail(event) {
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?orderId=' + event.currentTarget.dataset.id,
    })
  },
  //再来一单
  onRepetitionOrder(event) {
    console.log(event);
    api.orderRepetition(event.currentTarget.dataset.id).then(res => {
      if (res.code == 1) {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
  },
  //支付订单
  onPayOrder(event) {
    console.log(event);
    wx.navigateTo({
      url: '/pages/pay/pay?number=' + event.currentTarget.dataset.number,
    })
  },
  //删除订单
  onDeleteOrder(event) {
    // console.log(event);
    api.deleteOrder(event.currentTarget.dataset.id).then(res => {
      if (res.code == 1) {
        wx.showToast({
          title: '删除成功',
          icon: 'success'
        })
        let orderList = this.data.orderList
        orderList.splice(event.currentTarget.dataset.index, 1);
        this.setData({
          orderList
        })
      }
    })
  },
  lower(e) {
    console.log(e)
    if (!this.data.isRequest) {
      return;
    } else {
      let page = this.data.page + 1;
      if (this.data.status == "") {
        this.orderHistory(page, this.data.pageSize)
      } else if (this.data.status == 2) {
        this.orderHistory(page, this.data.pageSize, 2)
      }
      else if (this.data.status == 5) {
        this.orderHistory(page, this.data.pageSize, 5)
      } else {
        this.orderHistory(page, this.data.pageSize, 6)
      }
      this.setData({
        page
      })
    }

  },
  //获取订单列表
  orderHistory(page, pageSize, status) {
    let orderList = this.data.orderList.length;
    let newOrderList = this.data.orderList;
    let data = {
      page: page,
      pageSize: pageSize,
      status: status == undefined ? "" : status,
    }
    wx.showLoading({
      title: '加载中...',
    })
    api.orderHistory(data).then(res => {
      if (res.code == 1) {
        for (let i = 0; i < res.data.records.length; i++) {
          let j = i + orderList
          newOrderList[j] = res.data.records[i]
        }
        this.setData({
          orderList: newOrderList
        })
      }
      if (res.data.records.length < 10) {
        this.setData({
          isRequest: false,
        })
      }
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.orderHistory(this.data.page, this.data.pageSize)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})